/*
 * Copyright (C) 2011-2013 Eric Lasota
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "rdx_objectloader.hpp"
#include "rdx_io.hpp"

namespace RDX
{
	namespace Utility
	{
		using namespace RDX::Programmability;
		using namespace RDX::ObjectManagement;

		RuntimeObjectLoaderHost::RuntimeObjectLoaderHost(IPackageHost *parent, Domain domain, IO::IFileSystem *fs)
		{
			_parentHost = parent;
			_runtimePackagePath = NULL;
			_runtimePackageIsText = false;
			_emulatedDomain = domain;
			_fs = fs;
		}

		void RuntimeObjectLoaderHost::SetPackage(const Char *packagePath, bool isText)
		{
			_runtimePackagePath = packagePath;
			_runtimePackageIsText = isText;
		}

		Domain RuntimeObjectLoaderHost::DomainForSymbolName(IObjectManager *om, const String *symbol, Domain domain)
		{
			return _parentHost->DomainForSymbolName(om, symbol, domain);
		}

		IO::IFileStream *RuntimeObjectLoaderHost::StreamForDomain(IObjectManager *om, Domain domain, bool write, bool &isText)
		{
			if(write == false && domain == DOMAIN_Runtime && _runtimePackagePath)
			{
				IO::IFileStream *stream = _fs->Open(_runtimePackagePath, false);
				if(!stream)
					return NULL;

				isText = _runtimePackageIsText;
				return stream;
			}

			return _parentHost->StreamForDomain(om, domain, write, isText);
		}

		bool RuntimeObjectLoaderHost::DomainsVisible(Domain sourceDomain, Domain destDomain)
		{
			if(sourceDomain == DOMAIN_Runtime)
				sourceDomain = _emulatedDomain;
			return _parentHost->DomainsVisible(sourceDomain, destDomain);
		}

		bool RuntimeObjectLoaderHost::DomainCanContainMethods(Domain domain)
		{
			return _parentHost->DomainCanContainMethods(domain);
		}
	}
}
